package com.suvel.study.controller;

import com.suvel.study.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
@RefreshScope
public class CodeController {

    @Autowired
    private CodeService codeService;

    @Value("${code.message}")
    private String codeMessage;

    @GetMapping("/create/{email}")
    public Boolean create(@PathVariable String email) {
        boolean result = true;
        try {
            codeService.createAndSend(email);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }

    @GetMapping("/validate/{email}/{code}")
    public Integer validate(@PathVariable String email, @PathVariable String code) {
        Integer status = 1;
        try {
            status = codeService.validate(email, code);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return status;
    }

    @GetMapping("/getMessage")
    public String getMessage() {
        System.out.println(codeMessage);
        return codeMessage;
    }

}
